package com.newcoder.util;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.HashMap;
import java.util.Map;


@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class ResponseWrapper {
    /**是否成功*/
    private boolean success;
    /**返回码*/
    private Integer code;
    /**返回信息*/
    private String msg;
    /**返回数据*/
//    private Object data;
    private Map data = new HashMap<String, Object>();

    /**
     * 数据data通过以下设置：
     *ResponseWrapper wrapper = ResponseWrapper.markSuccess();
     *         wrapper.getData().put("data",list);                     //当前页数据
     *         wrapper.getData().put("total", total);                  //总数据个数
     *         wrapper.getData().put("currentPage", currentPage);      //当前页
     *         wrapper.getData().put("currentNum", currentNum);        //当前页第一个数据下标
     *         wrapper.getData().put("pageSize", pageSize);            //页大小
     *         wrapper.getData().put("pages", pages);                  //总页数
     * */


    /**
     * 自定义返回结果
     * 建议使用统一的返回结果，特殊情况可以使用此方法
     * @param success
     * @param code
     * @param msg
     * @return
     */
    public static ResponseWrapper markCustom(boolean success, Integer code, String msg){
        ResponseWrapper responseWrapper = new ResponseWrapper();
        responseWrapper.setSuccess(success);
        responseWrapper.setCode(code);
        responseWrapper.setMsg(msg);
        return responseWrapper;
    }

    /**
     * 查询成功且有数据
     * @return
     */
    public static ResponseWrapper markSuccess(){
        ResponseWrapper responseWrapper = new ResponseWrapper();
        responseWrapper.setSuccess(true);
        responseWrapper.setCode(ReturnCode.SUCCESS.getCode());
        responseWrapper.setMsg(ReturnCode.SUCCESS.getMsg());
        return responseWrapper;
    }


    /**
     * 查询失败  parameter
     * @return
     */
    public static ResponseWrapper markError(){
        ResponseWrapper responseWrapper = new ResponseWrapper();
        responseWrapper.setSuccess(false);
        responseWrapper.setCode(ReturnCode.FEAILED.getCode());
        responseWrapper.setMsg(ReturnCode.FEAILED.getMsg());
        responseWrapper.setData(null);
        return responseWrapper;
    }

    /**
     * 参数为空  parameter
     * @return
     */
    public static ResponseWrapper parameterError(String msg){
        ResponseWrapper responseWrapper = new ResponseWrapper();
        responseWrapper.setSuccess(false);
        responseWrapper.setCode(ReturnCode.FEAILED.getCode());
        responseWrapper.setMsg(msg);
        responseWrapper.setData(null);
        return responseWrapper;
    }


    /**
     * 异常处理
     */
    public static ResponseWrapper exceptionError(Integer code, String msg){
        ResponseWrapper responseWrapper = new ResponseWrapper();
        responseWrapper.setSuccess(false);
        responseWrapper.setCode(code);
        responseWrapper.setMsg(msg);
        return responseWrapper;
    }

}